from collections import OrderedDict

from django.db.models.query import QuerySet
from rest_framework.generics import ListAPIView, CreateAPIView, ListCreateAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

from meiduo_admin.serializers.user import UserSerializer
from meiduo_admin.utils import PageNum
from users.models import User


# 用户管理
class UserListView(ListCreateAPIView):
    queryset = User.objects.all().order_by('id')
    serializer_class = UserSerializer

    # 分页类
    pagination_class = PageNum

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')
        if not (keyword is None or keyword == ''):
            return User.objects.filter(username__contains=keyword).order_by('id')

        queryset = self.queryset
        if isinstance(queryset, QuerySet):
            # Ensure queryset is re-evaluated on each request.
            queryset = queryset.all()
        return queryset

